package org.skydiveapp2;

import org.skydiveapplibrary.data.MoreCategory;
import org.skydiveapplibrary.ui.MoreCategoryAdapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class MoreActivity extends BaseListFragment
{
	// activity request codes
	 private static final int REQUEST_IMPORT_EXPORT = 0;
	 
	// listeners
	private OnItemClickListener itemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		{
			// get selected category
			MoreCategoryAdapter adapter = (MoreCategoryAdapter)getListAdapter();
			MoreCategory category = (MoreCategory)adapter.getItem(position);
	    	// handle selection
			handleSelection(category);
		}
	};
	
	@Override
	public void onResume()
	{
		super.onResume();
		
		// set adapter
		setListAdapter(new MoreCategoryAdapter(getActivity()));
		
		// set header
		setSubtitleText(R.string.main_more_tab_label);
		
		// hide tablet frame
		hideTabletSpacer();
		
		getListView().setOnItemClickListener(itemClickListener);
	}
	
	private void handleSelection(MoreCategory category)
	{
		if (category == MoreCategory.Aircraft)
		{	
			Intent intent = new Intent(getActivity(), AircraftListActivity.class);
            startActivity(intent);
		}
		else if (category == MoreCategory.SkydiveTypes)
		{
			Intent intent = new Intent(getActivity(), SkydiveTypeListActivity.class);
            startActivity(intent);
		}
		else if (category == MoreCategory.Locations)
		{			
			Intent intent = new Intent(getActivity(), LocationListActivity.class);
            startActivity(intent);
		}
		else if (category == MoreCategory.WingLoadCalculator)
		{		
			Intent intent = new Intent(getActivity(), WingLoadCalculatorActivity.class);
            startActivity(intent);
		}
		else if (category == MoreCategory.ImportExport)
		{
			Intent intent = new Intent(getActivity(), ImportExportActivity.class);
            startActivityForResult(intent, REQUEST_IMPORT_EXPORT);
		}
		else if (category == MoreCategory.Settings)
		{
			Intent intent = new Intent(getActivity(), SettingsActivity.class);
            startActivity(intent);
		}
	}	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
            if (requestCode == REQUEST_IMPORT_EXPORT && resultCode == Activity.RESULT_OK)
            {
            	((MainActivity) getActivity()).updateLogEntryListView();
            }
            else
            	super.onActivityResult(requestCode, resultCode, intent);
    }
}
